package com.renbang.utils;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;

import com.renbang.dao.sqlsession.DBUtils;
import com.renbang.dao.sqlsession.DaoImp;
import com.renbang.model.common.PageData;
public class SelectTag extends TagSupport implements Serializable{  
    private static final long serialVersionUID = 1L;
    DaoImp dao = new DaoImp();
    //表名，字段名，条件
    private String tabName = "";  
    private String where = "";
    private String filed = "";
    private String value = ""; 
    private String inputname = "";
    private String onchange = "";
    
	public String getOnchange() {
		return onchange;
	}
	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}
	public String getInputname() {
		return inputname;
	}
	public void setInputname(String inputname) {
		this.inputname = inputname;
	}
	public DaoImp getDao() {
		return dao;
	}
	public void setDao(DaoImp dao) {
		this.dao = dao;
	}
	public String getTablename() {
		return tabName;
	}
	public String getTabName() {
		return tabName;
	}
	public void setTabName(String tabName) {
		this.tabName = tabName;
	}
	public String getWhere() {
		return where;
	}
	public void setWhere(String where) {
		this.where = where;
	}
	public String getFiled() {
		return filed;
	}
	public void setFiled(String filed) {
		this.filed = filed;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public SelectTag() {  
    }  
    public int doStartTag() {  
    		//SKIP_BODY
        return EVAL_BODY_INCLUDE;  
    }  
    public int doEndTag() throws JspException {
    	try {
    		List<PageData> list = DBUtils.getList(tabName,value+" , "+filed,where);
    		JspWriter wite = this.pageContext.getOut();
    		wite.write("<select name='"+this.inputname+"' onchange= '"+this.onchange+"' style='width:150px;margin-left:20px;'>");
    		wite.write("<option value=''>");
			wite.write("全部");
			wite.write("</option>");
    		for (PageData pd : list) {
    			//拼下拉框
    			/*System.out.println(pd.getString(value));
    			System.out.println(pd.getString(filed));*/
    			wite.write("<option value='"+pd.getString(value)+"'>");
    			wite.write(pd.getString(filed));
    			wite.write("</option>");
				//便利map
    			/*while(ite.hasNext()){
    				//<option></option>
    				//
    					Object obj = ite.next();
    					wite.write("<option value='"+this.inputname+"'>");
    				System.out.println("KEY:"+obj);
    				System.out.println("VALUE:"+pd.get(obj));
    			}*/
			}
    		wite.write("</select>");
		} catch (Exception e) {
			e.printStackTrace();
		}
        return EVAL_PAGE;  
    }  
}  
